package com.quickcode.example.concurrent;

import com.quickcode.annotation.Safe;
import lombok.AllArgsConstructor;
import lombok.Data;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.PriorityBlockingQueue;


/**
 * 顺序队列，需实现Comparable接口
 */
@Safe
public class PriorityBlockingQueueExample {

    public static void main(String[] args) {
        BlockingQueue<TestClass> priorityBlockingQueue = new PriorityBlockingQueue();
        TestClass t1 = new TestClass("zhangsan",20);
        TestClass t2 = new TestClass("lisi",18);
        TestClass t3 = new TestClass("wangwu",25);
        priorityBlockingQueue.offer(t1);
        priorityBlockingQueue.offer(t2);
        priorityBlockingQueue.offer(t3);

        System.out.println(priorityBlockingQueue.poll().getName());
        System.out.println(priorityBlockingQueue.poll().getName());
        System.out.println(priorityBlockingQueue.poll().getName());

    }


    @Data
    @AllArgsConstructor
    static class TestClass implements Comparable<TestClass> {

        private String name;

        private int age;

        @Override
        public int compareTo(TestClass o) {
            if(this.age > o.getAge()) {
                return 1;
            }
            return -1;
        }

    }
}